using System;
using System.Collections.Generic;

namespace KodeIT.Web
{
    /// <summary>
    /// This class represents the abstract event raised by IIS whenever there is access to the request 
    /// or response headers.
    /// </summary>
    public abstract class HeadersEvent : ContextEvent
    {
        private HttpHeaders _headers;
        private Dictionary<string, string> _setCollection;

        internal HeadersEvent(NativeContext nativeContext)
            : base(nativeContext)
        {
            _setCollection = new Dictionary<string, string>();
            _headers = new HttpHeaders(nativeContext, _setCollection);
        }

        /// <summary>
        /// Gets the collection of <see cref="HttpHeaders"/> of the current request or response.
        /// </summary>
        public HttpHeaders Headers
        {
            get { return _headers; }
        }

        internal override FilterStatus Update()
        {
            foreach (string headerName in _setCollection.Keys)
            {
                NativeMethods.SetHeader(
                    NativeContext,
                    headerName,
                    _setCollection[headerName]);
            }

            return base.Update();
        }
    }
}
